import { topCategory } from '@/api/constants'
import Category from '@/api/modules/category'
export default {
  namespaced: true,
  state() {
    return {
      // 填充假数据, 防止初次渲染空白(和接口返回真实数据结构要一致)
      list: topCategory.map((item) => ({ name: item }))
    }
  },
  mutations: {
    setList(state, payload) {
      state.list = payload
    },
    // 鼠标点击是否显示 推荐商品
    show(state, id) {
      const categoryObj = state.list.find((item) => item.id === id)
      if (categoryObj !== -1) categoryObj.open = true
    },
    hidden(state, id) {
      const categoryObj = state.list.find((item) => item.id === id)
      if (categoryObj !== -1) categoryObj.open = false
    }
  },
  actions: {
    async getList({ commit }) {
      let { code, result } = await Category.categoryList()
      if (code == 1) {
        result.forEach((item) => (item.isOpen = false))
        commit('setList', result)
      }
    }
  }
}
